package com.feng.eduservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.feng.commonutils.R;
import com.feng.eduservice.entity.EduChapter;
import com.feng.eduservice.entity.EduVideo;
import com.feng.eduservice.mapper.EduChapterMapper;
import com.feng.eduservice.service.EduChapterService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.feng.eduservice.service.EduVideoService;
import com.feng.eduservice.util.BeanCopyUtils;
import com.feng.eduservice.vo.course.chapterVo;
import com.feng.eduservice.vo.course.videoVo;
import com.feng.servicebase.excecption.GuliException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author fengge
 * @since 2023-03-16
 */
@Service
public class EduChapterServiceImpl extends ServiceImpl<EduChapterMapper, EduChapter> implements EduChapterService {
    @Autowired
    private EduVideoService eduVideoService;
    @Autowired
    private EduChapterService eduChapterService;
    @Override
    public List<chapterVo> getChapterVideo(String courseId) {
        //条件查询
        LambdaQueryWrapper<EduChapter> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(EduChapter::getCourseId,courseId);
        queryWrapper.orderByAsc(EduChapter::getSort);
        //得到课程所对应的章节信息
        List<EduChapter> list = this.list(queryWrapper);
        List<chapterVo> chapterVos=new ArrayList<>();
        for (EduChapter eduChapter : list) {
            chapterVo chapterVo = BeanCopyUtils.copyBean(eduChapter, chapterVo.class);
            chapterVos.add(chapterVo);
        }
        LambdaQueryWrapper<EduVideo> queryWrapper1=new LambdaQueryWrapper<>();
        queryWrapper1.eq(EduVideo::getCourseId,courseId);
        //得到对应课程id的课程小节信息
        List<EduVideo> list1 = eduVideoService.list(queryWrapper1);
       chapterVos.stream().map(e->{
           //为每一个章节设置每一个小节
          List<EduVideo> eduVideos=  setChildren(e,list1);
           List<videoVo> videoVos = BeanCopyUtils.copyBeanList(eduVideos, videoVo.class);
           e.setChildren(videoVos);
           return e;
       }).collect(Collectors.toList());
        return chapterVos;


    }

    @Override
    public R addChapter(EduChapter eduChapter) {
        eduChapterService.save(eduChapter);

        return R.ok();

    }

    @Override
    public R deleteChapter(String chapterId) {
        LambdaQueryWrapper<EduVideo> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(EduVideo::getChapterId,chapterId);
        List<EduVideo> list = eduVideoService.list(queryWrapper);
        if(list.size()!=0){
            throw new GuliException(20001,"该章节关联着小节数据,不能进行删除");
        }
        else{
            this.removeById(chapterId);
            return R.ok().message("删除章节成功");
        }
    }

    private List<EduVideo> setChildren(chapterVo e, List<EduVideo> list1) {
        List<EduVideo> collect = list1.stream().filter(a -> {
            return a.getChapterId().equals(e.getId());
        }).collect(Collectors.toList());
        return collect;

    }
}
